package org.jeecg.modules.travel.controller;

import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.travel.dto.likes.AppLikePageDto;
import org.jeecg.modules.travel.dto.likes.LikeAddDto;
import org.jeecg.modules.travel.dto.likes.PcLikesPageDto;
import org.jeecg.modules.travel.entity.JhLikes;
import org.jeecg.modules.travel.service.IJhLikesService;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.travel.vo.likes.AppLikeRecordVo;
import org.jeecg.modules.travel.vo.likes.PcLikesRecordVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

 /**
 * @Description: 用户点赞表
 * @Author: jeecg-boot
 * @Date:   2025-04-19
 * @Version: V1.0
 */
@Tag(name="用户点赞表")
@RestController
@RequestMapping("/travel/jhLikes")
@Slf4j
public class JhLikesController extends JeecgController<JhLikes, IJhLikesService> {

	@Autowired
	private IJhLikesService jhLikesService;

	/**
	 * 分页列表查询  todo 新增realName返回字段，vo  待调整-不在controller里面写
	 *
	 * @param dto
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "用户点赞表-分页列表查询")
	@Operation(summary="用户点赞表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<PcLikesRecordVo>> queryPageList(PcLikesPageDto dto, HttpServletRequest req) {
		JhLikes likes = new JhLikes();
		BeanUtils.copyProperties(dto, likes);
        QueryWrapper<JhLikes> queryWrapper = QueryGenerator.initQueryWrapper(likes, req.getParameterMap());
		Page<JhLikes> page = new Page<JhLikes>(dto.getPageNo(), dto.getPageSize());
		return Result.OK(jhLikesService.queryPageList(page, queryWrapper));
	}

	
	/**
	 *   添加
	 *
	 * @param jhLikes
	 * @return
	 */
	@AutoLog(value = "用户点赞表-添加")
	@Operation(summary="用户点赞表-添加")
	@RequiresPermissions("org:jh_likes:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody JhLikes jhLikes) {
		jhLikesService.save(jhLikes);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param jhLikes
	 * @return
	 */
	@AutoLog(value = "用户点赞表-编辑")
	@Operation(summary="用户点赞表-编辑")
	@RequiresPermissions("org:jh_likes:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody JhLikes jhLikes) {
		jhLikesService.updateById(jhLikes);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "用户点赞表-通过id删除")
	@Operation(summary="用户点赞表-通过id删除")
	@RequiresPermissions("org:jh_likes:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		jhLikesService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "用户点赞表-批量删除")
	@Operation(summary="用户点赞表-批量删除")
	@RequiresPermissions("org:jh_likes:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.jhLikesService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "用户点赞表-通过id查询")
	@Operation(summary="用户点赞表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<JhLikes> queryById(@RequestParam(name="id",required=true) String id) {
		JhLikes jhLikes = jhLikesService.getById(id);
		if(jhLikes==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(jhLikes);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param jhLikes
    */
    @RequiresPermissions("org:jh_likes:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, JhLikes jhLikes) {
        return super.exportXls(request, jhLikes, JhLikes.class, "用户点赞表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequiresPermissions("org:jh_likes:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, JhLikes.class);
    }

	/* =============================== 移动端服务 ============================================ */


	 @AutoLog(value = "用户点赞表-移动端分页列表查询")
	 @Operation(summary="用户点赞表-移动端分页列表查询")
	 //@RequiresRoles(value = {"app"})
	 //@RequiresPermissions("app:jh_likes:appPageList")
	 @GetMapping(value = "/app/appPageList")
	 public Result<IPage<AppLikeRecordVo>> appPageList(AppLikePageDto dto, HttpServletRequest req) {
		 JhLikes likes = new JhLikes();
		 BeanUtils.copyProperties(dto,likes);
		 String userId = getUserId();
		 QueryWrapper<JhLikes> queryWrapper = QueryGenerator.initQueryWrapper(likes, req.getParameterMap());
		 queryWrapper.lambda().eq(StringUtils.isNotBlank(userId), JhLikes::getUserId,userId);
		 Page<JhLikes> page = new Page<JhLikes>(dto.getPageNo(), dto.getPageSize());
		 return Result.OK(jhLikesService.appPageList(dto,page, queryWrapper));
	 }


	@AutoLog(value = "用户点赞表-移动端点赞")
	@Operation(summary="用户点赞表-移动端点赞")
	//@RequiresRoles(value = {"app"})
	//@RequiresPermissions("app:jh_likes:giveThumbsUp")
	@PostMapping(value = "/app/giveThumbsUp")
	public Result<String> giveThumbsUp(@RequestBody @Valid LikeAddDto dto) {
		dto.setUserId(getUserId());
		String res = jhLikesService.giveThumbsUp(dto);
		if (StringUtils.isNotBlank(res)) {
			return Result.error(res);
		}
		return Result.OK("点赞成功！");
	}

	@AutoLog(value = "用户点赞表-移动端取消点赞")
	@Operation(summary="用户点赞表-移动端取消点赞")
	//@RequiresRoles(value = {"app"})
	//@RequiresPermissions("app:jh_likes:cancelGiveThumbsUp")
	@GetMapping(value = "/app/cancelGiveThumbsUp")
	public Result<String> cancelGiveThumbsUp(@RequestParam(name="id",required=true) String id) {
		String res = jhLikesService.cancelLikes(id);
		if (StringUtils.isNotBlank(res)) {
			return Result.error(res);
		}
		return Result.OK("取消点赞成功！");
	}

	 @AutoLog(value = "用户点赞表-移动端取消点赞")
	 @Operation(summary="用户点赞表-移动端取消点赞")
	 //@RequiresRoles(value = {"app"})
	 //@RequiresPermissions("app:jh_likes:cancelGiveThumbsUp")
	 @GetMapping(value = "/app/cancelContent")
	 public Result<String> cancelContent(@RequestParam(name="contentId",required=true) String contentId,
											  @RequestParam(name="type",required=true) String type) {
		 String res = jhLikesService.cancelContent(contentId,type,getUserId());
		 if (StringUtils.isNotBlank(res)) {
			 return Result.error(res);
		 }
		 return Result.OK("取消点赞成功！");
	 }

}
